package com.hb.game.pk.ui.activity.mine;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.utils.LogUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.wv_web)
    WebView wvWeb;
    String load_url;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.this.finish();
            }
        });
        load_url = Host.HOST + "/app/ucenter/haibao?uid=" + APP.uid;
        LogUtils.d(load_url);
        initWeb();
    }

    private void initWeb() {
        WebSettings webSetting = wvWeb.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        webSetting.setSupportZoom(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setLoadWithOverviewMode(true);

        WebChromeClient client = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                if (!StringUtils.isEmpty(title)) {
//                    if (title.length()>4){
//                        title=title.substring(0,4);
//                    }
//                }
            }
        };

        wvWeb.setWebChromeClient(client);
        Map<String, String> map = new HashMap<String, String>();
        map.put("token", APP.Token);
        map.put("userid", APP.uid);
        wvWeb.loadUrl(load_url, map);
        wvWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
    }
}
